# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： tmp_lg-01.py
# @Time：2023/8/4 17:02
# @Author：liaogan
import allure

from common.excel.excel import read_all_caseexcel
from config.conf import BASE_DIR
from conftest import yg_login_jh_backstage
from common.log.log import log
from po.jinhua.ht.commodity_management_page.commodity_management_page import CommodityManagementPage

from po.jinhua.ht.jinhua_ht_main.jinhua_ht_main_page import JHHTHomePage
from po.public_methods import yg_ui_login_backstage, yk_tt_ui_login
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase


class JHHTHomePage(JHBase):
    """
    金花商城后台主页
    """

    # @allure.step('进入商品管理菜单页')
    def go_to_commodity_management_page(self):
        """
        进入商品管理菜单页
        :return:
        """
        try:
            log.info('进入商品管理菜单页')
            self.open_menu_2('商品管理')
            return CommodityManagementPage(self.driver)
        except Exception as e:
            log.error(f'进入商品管理菜单页，异常为{e}')
            raise e

    @allure.step('管理台打开指定的菜单')
    def open_menu(self, platform, name):
        """
        管理台打开指定的菜单
        platform:平台
        menu_name:菜单名
        """
        try:
            # 当前测试请求的url
            test_url = self.driver.current_url
            print(test_url)
            # 获取host
            url = test_url.split('/#')[0]
            dict_menu = {}  # 放菜单和路由对应关系
            if platform == 'yg':
                file = BASE_DIR + '/TestCase/test_huawang_yg/test_ht_ui/test_menu_page/test_huawang_yk_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                print(data,ids)
                # 取路由
                for i in range(len(data)):
                    print("i为",i)
                    menu_name = data[i][-1]  # 菜单名
                    print("菜单为：",menu_name)
                    path_route = data[i][1]  # 路由
                    print("路由为:",path_route)
                    dict_menu[menu_name] = path_route
                    print("字典为：",dict_menu)
                self.driver.get(url + dict_menu[name])
            elif platform == 'yk':
                file = BASE_DIR + '/TestCase/test_huawang_yk/test_ht_ui/test_menu_page/test_huawang_yk_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                print(data,ids)
                # 取路由
                print(len(data))
                for i in range(len(data)):
                    menu_name = data[i][-1]  # 菜单名
                    print("菜单名：",menu_name)
                    path_route = data[i][1]  # 路由
                    print("路由为",path_route)
                    dict_menu[menu_name] = path_route
                    print('字典为：',dict_menu)
                    # print("路由地址为：",url + dict_menu[name])
                self.driver.get(url + dict_menu[name])

            elif platform == 'yc':
                file = BASE_DIR + '/TestCase/test_huawang_yc/test_ht_ui/test_menu_page/test_huawang_yc_ht_menu_page.xlsx'
                data, ids = read_all_caseexcel(file)
                # 取路由
                for i in range(len(data)):
                    menu_name = data[i][-1]  # 菜单名
                    path_route = data[i][1]  # 路由
                    dict_menu[menu_name] = path_route
                self.driver.get(url + dict_menu[name])
        except Exception as e:
            log.error(f'打开菜单失败，异常为{e}')
            raise e




if __name__ == '__main__':
    # drive = yg_ui_login_backstage(mode=1)
    # JHHTHomePage(drive).open_menu('yg', '商品管理')
    #
    drive = yk_tt_ui_login(mode=1)
    JHHTHomePage(drive).open_menu('yk','订单')
    # add_tips = CommodityManagementPage(drive).add_item(item_name).get_tips()
    # assert add_tips == '新增成功'
